/**
 * 
 */
package com.base.myproject.client.busi.system;

import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * 用户自己的信息。提供修改密码
 * 
 * @author zgw 2009-11-10 下午07:06:52 javazgw@gmail.com
 */
public class Bcodeself {

	String ccode;

	TextField<String> oldpass;
	TextField<String> pass;
	TextField<String> Confirmpass;

	public Bcodeself(String bcode) {
		this.ccode = bcode;

	}
	
	int num =3;
	Dialog d = new Dialog();
	public void showDialog() {
		// Env.setEnvbcode("999");
		// Env.setEnvname("guest");
		
		d.setWidth("100");
		d.setTitle("修改密码");
		d.setHeading("修改密码");

		final FormPanel formpanel = new FormPanel();

		// final TextField<String> ccode = new TextField<String>();
		// ccode.setAllowBlank(false);
		// ccode.setFieldLabel("用户");

		// LabelField bcode = new LabelField();
		// bcode.setValue(Env.getEnvname());
		// bcode.setFieldLabel("用户名");

		final TextField<String> bcode = new TextField<String>();
		bcode.setValue(Env.getEnvname());
		bcode.setFieldLabel("雇员姓名");
		bcode.setEnabled(false);

		oldpass = new TextField<String>();
		oldpass.setAllowBlank(false);
		oldpass.setFieldLabel("旧密码");
		oldpass.setPassword(true);

		pass = new TextField<String>();
		pass.setAllowBlank(false);
		pass.setFieldLabel("密码");
		pass.setPassword(true);

		Confirmpass = new TextField<String>();
		Confirmpass.setAllowBlank(false);
		Confirmpass.setFieldLabel("确认密码");
		Confirmpass.setPassword(true);

		formpanel.setLabelWidth(75);
		formpanel.setFieldWidth(100);

		formpanel.add(bcode);
		formpanel.add(oldpass);
		formpanel.add(pass);
		formpanel.add(Confirmpass);

		d.setSize(250, 220);
		d.add(formpanel);
		d.setButtons(Dialog.OKCANCEL);

		d.getButtonById(Dialog.CANCEL).addSelectionListener(
				new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						d.setVisible(false);
					}
				});
		d.getButtonById(Dialog.OK).addSelectionListener(
				new SelectionListener<ButtonEvent>() {
					@Override
					public void componentSelected(ButtonEvent ce) {
						if (oldpass.getRawValue().equals("")) {
							MessageBox.alert("提示", "请输入旧密码，谢谢", null);
							return;
						}
						if (pass.getRawValue().equals("")) {
							MessageBox.alert("提示", "请输入新密码,谢谢", null);
							return;
						}
						if (Confirmpass.getRawValue().equals("")) {
							MessageBox.alert("提示", "请输入确认密码，谢谢", null);
							return;
						}
						if (!pass.getRawValue().equals(Confirmpass.getRawValue())) {
							MessageBox.alert("提示", "新密码和确认密码不一致，新重新输入，谢谢", null);
							pass.setValue("");
							Confirmpass.setValue("");
							return;
						}
						
						final String bcode = Env.getEnvbcode();

						String sql = "select password from bcode where bcode='"
								+ bcode + "' and password='" + oldpass.getRawValue()
								+ "'";
						final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
								.create(com.base.myproject.client.tools.GreetingService.class);
						greetingService2.getDataSet(sql,
								new AsyncCallback<DataSet>() {

									@Override
									public void onFailure(Throwable caught) {
									}

									@Override
									public void onSuccess(DataSet result) {
										num--;
										if (num <= 0) {
											Window.alert("你输入的密码次数超过3次，此次登陆不能再修改密码了");
											d.setVisible(false);
											return;
										}
										if (result.getRowCount() > 0) {
											String sql = "update bcode set password = '"
												+ Confirmpass.getRawValue()
												+ "' where bcode = '"
												+ bcode
												+ "'";
											updatePassword(sql);
										} else {
											Window.alert("你输入的密码有误，请重新输入，你还可以输入" + num + "次");
										}
									}
								});

					}

				});

		d.show();
	}
	
	public boolean updatePassword(String sql) {
		CommandSyncsql csql = new CommandSyncsql();// 执行sql命令
		csql.getV().add(sql);
		if (csql.getV().size() <= 0) {
			return false;
		} else {
			CommandSyncContainer list = new CommandSyncContainer();// 可以添加的命令
			list.add(csql);

			GreetingServiceAsync greetingService = GWT
					.create(GreetingService.class);
			greetingService.SendCommandSync(list,
					new AsyncCallback<CommandSyncReturnObject>() {
						@Override
						public void onFailure(Throwable caught) {
							MessageBox.alert("错误", "网络连接失败", null);
						}

						@Override
						public void onSuccess(CommandSyncReturnObject result) {
							if (result.isB()) {
								Info.display("提示信息", "修改密码成功");
								d.setVisible(false);
							} else {
								MessageBox.alert("错误", "错误，错误代码"
										+ result.getMessage(), null);

							}
						}
					});
		}
		return true;
	}
}
